using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using Catastrophe.Log.UI;

namespace Catastrophe.Log
{
	public class ExceptionUtil
	{
		public const string Split = "----------------------------------------";

		public static string ShowDialog(Exception ex, string apendFirst = "", string apendLast = "")
		{
			Tuple<string, List<ExceptionDetails>> tuple = ExceptionUtil.ExceptionFormat(ex, apendFirst, apendLast);
			StringBuilder stringBuilder = new StringBuilder();
			IEnumerable<string> enumerable = tuple.Item2.Select((ExceptionDetails x) => x.Message).Distinct();
			foreach (string item in enumerable)
			{
				stringBuilder.Append(item + Environment.NewLine + "----------------------------------------" + Environment.NewLine);
			}
            MessageBox.Show(stringBuilder.ToString(), "未处理异常",MessageBoxButton.OK);
            //new MessageDialog(LogLevelEnum.Error, stringBuilder.ToString(), tuple.Item1, "未处理异常").ShowDialog();
			return tuple.Item1;
		}

		public static void ThrowEx(Exception ex, string apendFirst = "", string apendLast = "")
		{
			Tuple<string, List<ExceptionDetails>> tuple = ExceptionUtil.ExceptionFormat(ex, apendFirst, apendLast);
			StringBuilder stringBuilder = new StringBuilder();
			IEnumerable<string> enumerable = tuple.Item2.Select((ExceptionDetails x) => x.Message).Distinct();
			foreach (string item in enumerable)
			{
				stringBuilder.Append(item + Environment.NewLine + "----------------------------------------" + Environment.NewLine);
			}
			throw new Exception(stringBuilder.ToString(), ex);
		}

		public static Tuple<string, List<ExceptionDetails>> ExceptionFormat(Exception ex, string apendFirst = "", string apendLast = "")
		{
			if (ex == null)
			{
				return new Tuple<string, List<ExceptionDetails>>("", new List<ExceptionDetails>());
			}
			string value = "";
			int index = 0;
			List<ExceptionDetails> details = new List<ExceptionDetails>();
			ExceptionUtil.ExceptionRecure(ref value, ex, ref index, ref details);
			string text = value;
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(3, 3);
			defaultInterpolatedStringHandler.AppendFormatted("----------------------------------------");
			defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
			defaultInterpolatedStringHandler.AppendLiteral("日期：");
			defaultInterpolatedStringHandler.AppendFormatted(DateTime.Now, "yyyy/MM/dd HH:mm:ss:fff");
			value = text + defaultInterpolatedStringHandler.ToStringAndClear();
			if (!string.IsNullOrWhiteSpace(apendFirst))
			{
				value = value.Insert(0, apendFirst + Environment.NewLine);
			}
			if (!string.IsNullOrWhiteSpace(apendLast))
			{
				value += apendLast;
			}
			return new Tuple<string, List<ExceptionDetails>>(value, details);
		}

		private static void ExceptionRecure(ref string value, Exception ex, ref int index, ref List<ExceptionDetails> details)
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (ex != null)
			{
				index++;
				ExceptionDetails exceptionDetails = new ExceptionDetails
				{
					Index = index,
					HelpLink = ex.HelpLink,
					HResult = ex.HResult,
					Source = ex.Source,
					StackTrace = ex.StackTrace,
					TargetSite = ex.TargetSite?.ToString(),
					Message = ex.Message
				};
				details.Add(exceptionDetails);
				string text = "----------------------------------------" + Environment.NewLine;
				value += text;
				StringBuilder stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder3 = stringBuilder2;
				StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(7, 2, stringBuilder2);
				handler.AppendLiteral("Index:");
				handler.AppendFormatted(index);
				handler.AppendLiteral("：");
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder3.Append(ref handler);
				stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder4 = stringBuilder2;
				handler = new StringBuilder.AppendInterpolatedStringHandler(5, 2, stringBuilder2);
				handler.AppendLiteral("异常内容:");
				handler.AppendFormatted(exceptionDetails.Message);
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder4.Append(ref handler);
				stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder5 = stringBuilder2;
				handler = new StringBuilder.AppendInterpolatedStringHandler(4, 2, stringBuilder2);
				handler.AppendLiteral("源信息:");
				handler.AppendFormatted(exceptionDetails.Source);
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder5.Append(ref handler);
				stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder6 = stringBuilder2;
				handler = new StringBuilder.AppendInterpolatedStringHandler(5, 2, stringBuilder2);
				handler.AppendLiteral("异常编码:");
				handler.AppendFormatted(exceptionDetails.HResult);
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder6.Append(ref handler);
				stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder7 = stringBuilder2;
				handler = new StringBuilder.AppendInterpolatedStringHandler(5, 2, stringBuilder2);
				handler.AppendLiteral("帮助连接:");
				handler.AppendFormatted(exceptionDetails.HelpLink);
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder7.Append(ref handler);
				stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder8 = stringBuilder2;
				handler = new StringBuilder.AppendInterpolatedStringHandler(5, 2, stringBuilder2);
				handler.AppendLiteral("堆栈信息:");
				handler.AppendFormatted(exceptionDetails.StackTrace);
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder8.Append(ref handler);
				stringBuilder2 = stringBuilder;
				StringBuilder stringBuilder9 = stringBuilder2;
				handler = new StringBuilder.AppendInterpolatedStringHandler(5, 2, stringBuilder2);
				handler.AppendLiteral("目标方法:");
				handler.AppendFormatted(exceptionDetails.TargetSite);
				handler.AppendFormatted(Environment.NewLine);
				stringBuilder9.Append(ref handler);
				value += stringBuilder.ToString();
				if (ex.InnerException != null)
				{
					ExceptionUtil.ExceptionRecure(ref value, ex.InnerException, ref index, ref details);
				}
			}
		}
	}
}
